package com.sokal.easycare.modules.product.web.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.product.entity.product.ProComment;
import com.sokal.easycare.modules.product.service.product.ProCommentService;

/**
 * 产品评论Controller
 * @author liliangdong
 * @version 2017-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/product/product/proComment")
public class ProCommentController extends BaseController {

	@Autowired
	private ProCommentService proCommentService;
	
	@ModelAttribute
	public ProComment get(@RequestParam(required=false) String id) {
		ProComment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = proCommentService.get(id);
		}
		if (entity == null){
			entity = new ProComment();
		}
		return entity;
	}
	
	@RequiresPermissions("product:product:proComment:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProComment proComment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProComment> page = proCommentService.findPage(new Page<ProComment>(request, response), proComment); 
		model.addAttribute("page", page);
		return "modules/product/product/proCommentList";
	}

	@RequiresPermissions("product:product:proComment:view")
	@RequestMapping(value = "form")
	public String form(ProComment proComment, Model model) {
		model.addAttribute("proComment", proComment);
		return "modules/product/product/proCommentForm";
	}

	@RequiresPermissions("product:product:proComment:edit")
	@RequestMapping(value = "save")
	public String save(ProComment proComment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proComment)){
			return form(proComment, model);
		}
		proCommentService.save(proComment);
		addMessage(redirectAttributes, "保存产品评论成功");
		return "redirect:"+Global.getAdminPath()+"/product/product/proComment/?repage";
	}
	
	@RequiresPermissions("product:product:proComment:edit")
	@RequestMapping(value = "delete")
	public String delete(ProComment proComment, RedirectAttributes redirectAttributes) {
		proCommentService.delete(proComment);
		addMessage(redirectAttributes, "删除产品评论成功");
		return "redirect:"+Global.getAdminPath()+"/product/product/proComment/?repage";
	}

}